package db

import (
	"camera_app/config"
	"fmt"
	"gorm.io/driver/mysql"
	"gorm.io/gorm"
	"gorm.io/gorm/schema"
)

func GetDatabaseInstance(path string) (db *gorm.DB, err error) {
	cnf, _ := (&config.Config{}).ReadConfig(path)
	db, err = gorm.Open(mysql.Open(fmt.Sprintf("%s:%s@tcp(%s:%d)/%s", cnf.User, cnf.Password, cnf.Url, cnf.Port, cnf.Db)),
		&gorm.Config{
			NamingStrategy: schema.NamingStrategy{
				SingularTable: true,
			},
		},
	)
	if err != nil {
		panic(err)
	}
	return
}
